//
//  SocketEngineSpec.m
//  Socket.IO-Client
//
//  Created by alanwang.dev on 16/8/8.
//  Copyright © 2016年 com.alanwang.dev. All rights reserved.
//

#import "SocketEngineSpec.h"

@implementation SocketEngineSpec

- (dispatch_queue_t)emitQueue{
  if (!_emitQueue) {
    _emitQueue = dispatch_queue_create("com.socketio.engineEmitQueue", DISPATCH_QUEUE_SERIAL);
  }
  return _emitQueue;
}

- (dispatch_queue_t)handleQueue{
  if (!_handleQueue) {
    _handleQueue = dispatch_queue_create("com.socketio.engineHandleQueue", DISPATCH_QUEUE_SERIAL);
  }
  return _handleQueue;
}

- (dispatch_queue_t)parseQueue{
  if (!_parseQueue) {
    _parseQueue = dispatch_queue_create("com.socketio.engineHandleQueue", DISPATCH_QUEUE_SERIAL);
  }
  return _parseQueue;
}

- (NSURL *)urlPollingWithSid{
    NSURLComponents *com = [NSURLComponents componentsWithURL:self.urlPolling resolvingAgainstBaseURL:NO];
    NSCharacterSet *chaSet = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]\" {}"] invertedSet];
    
    NSString *encodeStr = [self.sid stringByAddingPercentEncodingWithAllowedCharacters:chaSet];
    
    com.percentEncodedQuery = [NSString stringWithFormat:@"%@&sid=%@",com.percentEncodedQuery,encodeStr];
    
    return com.URL;
}

- (NSURL *)urlWebSocketWithSid{
    NSURLComponents *com = [NSURLComponents componentsWithURL:self.urlWebSocket resolvingAgainstBaseURL:NO];
    NSCharacterSet *chaSet = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]\" {}"] invertedSet];
    
    NSString *encodeStr = [self.sid stringByAddingPercentEncodingWithAllowedCharacters:chaSet];
    
    
    NSString *newSid = self.sid.length == 0 ? @"" : [NSString stringWithFormat:@"&sid=%@",encodeStr];
  if (newSid.length > 0) {
    if (com.query) {
      com.query = [NSString stringWithFormat:@"%@%@",com.query, newSid];
    } else {
      com.query = [NSString stringWithFormat:@"%@",newSid];
    }    
  }
  
    
    return com.URL;
}

- (Either *)createBinaryDataForSend:(NSData *)data{
    if (self.websocket) {
//        NSArray<UInt8> *byteArray =nil;
        UInt8 byteArray[1];
        byteArray[0] = 0x4;
        NSMutableData *mutData = [NSMutableData dataWithBytes:&byteArray length:1];
        
        [mutData appendData:data];
        
        Either *either = [Either new];
        either.Left = mutData;
        either.Right = nil;
        
        return either;
    }else{
        NSString *str = [NSString stringWithFormat:@"b4%@",[data base64EncodedStringWithOptions:0]];
        
        Either *either = [Either new];
        either.Left = nil;
        either.Right = str;
        
        return either;
    }
}

- (NSString *)doubleEncodeUTF8:(NSString *)string{
    NSData *latin1 = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSString *utf8 = [[NSString alloc] initWithData:latin1 encoding:NSISOLatin1StringEncoding];
    if (latin1 && utf8) {
        return utf8;
    }else{
        return string;
    }
}

- (NSString *)fixDoubleUTF8:(NSString *)string{
    NSData *utf8 = [string dataUsingEncoding:NSISOLatin1StringEncoding];
    NSString *latin1 = [[NSString alloc] initWithData:utf8 encoding:NSUTF8StringEncoding];
    if (utf8 && latin1) {
        return latin1;
    }else{
        return string;
    }
}

- (void)send:(NSString *)msg datas:(NSArray<NSData *> *)datas{
    [self write:msg type:PacketType_Message Data:datas];
}

@end